load("data_in.rdata")

## commented out for replication files 

#  install.packages("ggplot2")
#  install.packages("dplyr")
#  install.packages("devtools")
#  library(devtools)
#  devtools::install_github("sebastianbarfort/mapDK")

library(dplyr)
library(ggplot2)
library(mapDK)

data_in$par_municipality <-  gsub("\303\270", "oe", data_in$par_municipality)
data_in$par_municipality <-  gsub("\303\205", "Aa", data_in$par_municipality)
data_in$par_municipality <-  gsub("\303\246", "ae", data_in$par_municipality)
data_in$par_municipality <-  gsub("\303\245", "aa", data_in$par_municipality)
data_in$par_municipality <-  gsub("\303\206", "Ae", data_in$par_municipality)
data_in$par_municipality <-  gsub(" Kommune", "", data_in$par_municipality)
data_in$par_municipality <-  gsub("Ringbjoebing-Skjern", "Ringkoebing-Skjern", data_in$par_municipality)


data_in <- 
  data_in %>%
  arrange(in2009, in2013, in2014, in2015) %>%
  mutate(place = 1:nrow(.),
         zero  = 0,
         one   = 1,
         two   = 2,
         three = 3,
         four  = 4,
         sum   = in2009 + in2013 + in2014 + in2015)
         

in_plot <- 
  ggplot(data = subset(data_in, in2009 == TRUE), 
         aes(y = place, 
             x = zero, 
             xmin = zero,
             xmax = one)) +
  geom_errorbarh(height = 0,
                 alpha = 0.5) +
  geom_errorbarh(data = subset(data_in, in2013 == TRUE), 
                 aes(y = place, 
                     x = one, 
                     xmin = one,
                     xmax = two),
                 height = 0,
                 alpha = 0.5) +
  geom_errorbarh(data = subset(data_in, in2014 == TRUE), 
                 aes(y = place, 
                     x = two, 
                     xmin = two,
                     xmax = three),
                 height = 0,
                 alpha = 0.5) +
  geom_errorbarh(data = subset(data_in, in2015 == TRUE), 
                 aes(y = place, 
                     x = three, 
                     xmin = three,
                     xmax = four),
                 height = 0,
                 alpha = 0.5) +
  geom_errorbarh(data = subset(data_in, sum == 4), 
                 aes(y = place, 
                     x = zero, 
                     xmin = zero,
                     xmax = four),
                 height = 0) +
  theme_classic() +
  scale_x_continuous(limits = c(0,4),
                     breaks = seq(0.5,3.5,1),
                     labels = c(2009,2013,2014,2015)) +
  xlab("Election year") + 
  ylab("Municipality") + 
  scale_y_continuous(limits = c(1,98),
                     breaks = 1:98, 
                     labels = data_in$par_municipality)
  
in_plot

# Plot municipalities in each election ------------------------------------

data_map <- data_in
data_map$par_municipality <-  gsub("Koebenhavns", "Koebenhavn", data_map$par_municipality)
data_map$par_municipality <-  gsub("Bornholms", "Bornholm", data_map$par_municipality)
data_map$par_municipality <-  gsub("Ringkoebing-Skjern", "Ringkoebing skjern", data_map$par_municipality)

data_map <- 
  rbind(data_map, subset(data_map, par_municipality == "Bornholm")) 
data_map$par_municipality[99] <- "Christiansoe"
data_map$in2013[99] <- 0
data_map <- 
  data_map %>%
  mutate(in2009 = as.integer(in2009),
         in2013 = as.integer(in2013),
         in2014 = as.integer(in2014),
         in2015 = as.integer(in2015),
         all    = as.integer(sum == 4))

map2009 <- 
  mapDK(values = "in2009",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = c(0,1),
                        labels = c("Not included in data",
                                   "Included in data")) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("2009") 


# 2013 --------------------------------------------------------------------


map2013 <- 
  mapDK(values = "in2013",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = c(0,1),
                        labels = c("Not included in data",
                                   "Included in data")) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("2013") 


# 2014 --------------------------------------------------------------------


map2014 <- 
  mapDK(values = "in2014",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = c(0,1),
                        labels = c("Not included in data",
                                   "Included in data")) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("2014") 


# 2015 --------------------------------------------------------------------


map2015 <- 
  mapDK(values = "in2015",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = c(0,1),
                        labels = c("Not included in data",
                                   "Included in data")) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("2015") 

# 2009-2015 --------------------------------------------------------------------


mapAll <- 
  mapDK(values = "all",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = c(0,1),
                        labels = c("Not included in data",
                                   "Included in data")) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("Data for all elections, 2009-2015") 


mapCount <- 
  mapDK(values = "sum",
        id     = "par_municipality",
        data   = data_map) +
  scale_fill_continuous(guide = "legend", low = "gray", high = "black", 
                        breaks = 1:4,
                        labels = 1:4) +
  guides(fill=guide_legend(title="")) +
  theme(legend.text = element_text(size = 14),
        legend.position = "top",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("Elections with data, 2009-2015") 
